﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using Authorization;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Data;
using SharpObjects.Data.EntityMapping;
using SharpObjects.Instantiation.Services;
using SharpObjects.PathMapping;

namespace Authorization_Test
{
    class AuthorizationTest
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            Console.WriteLine("Current user: {0}", Thread.CurrentPrincipal.Identity.Name); 

            IConfigurationProvider config = new XmlConfigurationProvider("DbConfig.xml");
            ServiceFactory.Instance = config.GetObject<IServiceFactory>("//services");

            using(AccessControlReader accessControlReader = new AccessControlReader())
            {
                int id = accessControlReader.GetResourceIdByName("RESOURCE 1");
                List<AccessControlEntry> acls = accessControlReader.GetACLsByResourceId(id);
                acls = accessControlReader.GetACLsByResourceName("resource 1");
                acls = accessControlReader.GetACLsByResourceNameAndAccessorName("resource 1", "USER1");

                {
                    bool isAuthorized1 = accessControlReader.IsAuthorized(Thread.CurrentPrincipal, "resource 1");
                    bool isAuthorized2 = accessControlReader.IsAuthorized(Thread.CurrentPrincipal, "resource 2");
                    bool isAuthorized3 = accessControlReader.IsAuthorized(Thread.CurrentPrincipal, "resource 3");
                }

                IPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity("user1"), new string[]
                                                                                                   {
                                                                                                       "group1"
                                                                                                   });
                {
                    bool isAuthorized1 = accessControlReader.IsAuthorized(userPrincipal, "resource 1");
                    bool isAuthorized2 = accessControlReader.IsAuthorized(userPrincipal, "resource 2");
                    bool isAuthorized3 = accessControlReader.IsAuthorized(userPrincipal, "resource 3");
                }

                userPrincipal = new UserPrincipal(new UserIdentity("user2"), new List<string>() { "group2"});

                {
                    bool isAuthorized1 = accessControlReader.IsAuthorized(userPrincipal, "resource 1");
                    bool isAuthorized2 = accessControlReader.IsAuthorized(userPrincipal, "resource 2");
                    bool isAuthorized3 = accessControlReader.IsAuthorized(userPrincipal, "resource 3");
                }

            }


        }


    }
}
